/**
 * Copyright (c) 2018 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.validator;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import com.example.exception.BaseException;
import com.example.exception.ExceptionTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.Map;

/**
 * 校验工具类
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class AssertUtils {

    public static void isBlank(String str, String params) {
        isBlank(str, HttpStatus.BAD_REQUEST, params);
    }

    public static void isBlank(String str, HttpStatus code, String params) {
        if(code == null){
            ExceptionTool.throwException(params);
        }

        if (StringUtils.isBlank(str)) {
            throw new BaseException(params, code);
        }
    }

    public static void isNull(Object object, String params) {
        isNull(object, HttpStatus.BAD_REQUEST, params);
    }

    public static void isNull(Object object, HttpStatus code, String params) {
        if(code == null){
            ExceptionTool.throwException(params);
        }

        if (object == null) {
            throw new BaseException(params, code);
        }
    }

    public static void isArrayEmpty(Object[] array, String params) {
        isArrayEmpty(array, HttpStatus.BAD_REQUEST, params);
    }

    public static void isArrayEmpty(Object[] array, HttpStatus code, String params) {
        if(code == null){
            ExceptionTool.throwException(params);
        }

        if(ArrayUtil.isEmpty(array)){
            throw new BaseException(params, code);
        }
    }

    public static void isListEmpty(List<?> list, String params) {
        isListEmpty(list, HttpStatus.BAD_REQUEST, params);
    }

    public static void isListEmpty(List<?> list, HttpStatus code, String params) {
        if(code == null){
            ExceptionTool.throwException(params);
        }

        if(CollUtil.isEmpty(list)){
            throw new BaseException(params, code);
        }
    }

    public static void isMapEmpty(Map map, String params) {
        isMapEmpty(map, HttpStatus.BAD_REQUEST, params);
    }

    public static void isMapEmpty(Map map, HttpStatus code, String params) {
        if(code == null){
            ExceptionTool.throwException(params);
        }

        if(MapUtil.isEmpty(map)){
            throw new BaseException(params, code);
        }
    }

    public static void isMapEmptyMsg(Map map, String msg) {
        if(MapUtil.isEmpty(map)){
            throw new BaseException(msg);
        }
    }
}